<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-card>
          <div class="card-header">
            <span>新闻分类列表</span>
          </div>
          <el-form :inline="true" :model="searchForm">
            <el-form-item label="分类名称">
              <el-input v-model="searchForm.typeName" placeholder="请输入新闻分类名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="doSearch()">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row class="mt-10">
      <el-col :span="24">
        <el-card>
          <el-row>
            <el-col :span="24">
              <el-button type="success" @click="handleAdd()">新增</el-button>
            </el-col>
          </el-row>
          
          <el-table :data="pageData.data" border class="mt-10"
                    @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" label="编号" width="100"></el-table-column>
            <el-table-column prop="typeName" label="分类名称" width="180"></el-table-column>
            <el-table-column prop="remark" label="分类描述"></el-table-column>
            <el-table-column label="操作" width="300">
              <template #default="scope">
                <el-button size="mini"
                           @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button size="mini" type="danger"
                           @click="handleDelete(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          
          <el-pagination class="mt-10" @size-change="handleSizeChange"
                         @current-change="handleCurrentChange" :current-page="pageData.pageNo"
                         :page-sizes="[5, 10, 20, 50, 100]" :page-size="pageData.pageSize"
                         layout="total, sizes, prev, pager, next, jumper"
                         :total="pageData.totalCount">
          </el-pagination>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog :title="(saveForm.id == null ? '新增':'编辑') + '新闻分类'"
               v-model="saveDialogVisible">
      <el-form :model="saveForm" ref="saveFormRef" :rules="rules">
        <el-form-item label="分类ID" label-width="120px" v-show="false">
          <el-input type="hidden" v-model="saveForm.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类名称" label-width="120px" prop="typeName">
          <el-input v-model="saveForm.typeName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类描述" label-width="120px" prop="remark">
          <el-input v-model="saveForm.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="saveDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="doSave()">保 存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getNewsTypeList, getNewsTypeDetail, saveNewsType, deleteNewsType } from '@/api/newsType';

const searchForm = reactive({
  typeName: '',
  pageNo: 1,
  pageSize: 5
});

const pageData = reactive({
  data: [],
  pageNo: 1,
  pageSize: 10,
  totalCount: 0,
  totalPageCount: 1
});

const saveDialogVisible = ref(false);
const saveForm = reactive({
  id: null,
  typeName: '',
  remark: ''
});

const rules = reactive({
  typeName: [
    { required: true, message: '分类名称不能为空', trigger: 'blur' }
  ]
});

const multipleSelection = ref([]);
const saveFormRef = ref();

onMounted(() => {
  loadList();
});

const loadList = () => {
  getNewsTypeList({
    typeName: searchForm.typeName,
    pageNo: searchForm.pageNo,
    pageSize: searchForm.pageSize
  }).then(res => {
    if (res.code === 0) {
      Object.assign(pageData, res.data);
    } else {
      ElMessage.error(res.msg);
    }
  });
};

const doSearch = () => {
  searchForm.pageNo = 1;
  loadList();
};

const handleSizeChange = (size: number) => {
  searchForm.pageSize = size;
  loadList();
};

const handleCurrentChange = (page: number) => {
  searchForm.pageNo = page;
  loadList();
};

const handleAdd = () => {
  Object.assign(saveForm, { id: null, typeName: '', remark: '' });
  saveDialogVisible.value = true;
};

const handleEdit = (index: number, row: any) => {
  getNewsTypeDetail(row.id).then(res => {
    if (res.code === 0) {
      Object.assign(saveForm, res.data);
      saveDialogVisible.value = true;
    } else {
      ElMessage.error(res.msg);
    }
  });
};

const handleDelete = (index: number, row: any) => {
  ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    deleteNewsType(row.id).then(res => {
      if (res.code === 0) {
        ElMessage.success('删除成功');
        loadList();
      } else {
        ElMessage.error(res.msg);
      }
    });
  });
};

const doSave = () => {
  if (!saveFormRef.value) return;
  
  saveFormRef.value.validate((valid: boolean) => {
    if (!valid) return;
    saveNewsType(saveForm).then(res => {
      if (res.code === 0) {
        ElMessage.success('保存成功');
        saveDialogVisible.value = false;
        loadList();
      } else {
        ElMessage.error(res.msg);
      }
    });
  });
};

const handleSelectionChange = (val) => {
  multipleSelection.value = val;
};
</script>

<style>
.mt-10 {
  margin-top: 10px;
}
</style>